from threading import Thread
import time
import requests  # 导入requests包
import re  # 导入re包，正则表达式使用


ARK = {
    170 : [73043,0],
    576 : [3753870,0],
    312 : [1756005,0],
    618 : [5753765,0],
}

# 定义一个函数，作为新线程执行的入口函数
def server_people(server):
    try:
        url = f"https://www.battlemetrics.com/servers/ark/{ARK[server][0]}"  # 需要请求的网址
        html_str = requests.get(url)  # 发送Get请求
        # html_str 就是 html里面的所有内容
        people = re.findall(r"<dt>Player count</dt><dd>(.+?)</dd>",html_str.text)  # 通过正则表达式 r"<a.*?>.*?</a>" 找到所有的数据并输出
        server_name = 'server_name'
        if ARK[server][0] == 73043:
            server_name = 'EU-PVP-Official-TheIsland170 '
        elif ARK[server][0] == 1756005:
            server_name = "NA-PVP-Official-Aberration312"
        elif ARK[server][0] == 3753870:
            server_name = 'NA-PVP-Official-Valguero576  '
        elif ARK[server][0] == 5753765:
            server_name = 'NA-PVP-Official-GenOne618    '
        print(server_name, people[0], 'Refresh time:',
              time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
        # 123
        ARK[server][1] = people[0]
    except:
        pass

theadlist = []
for server in ARK:
    thread = Thread(target=server_people,
                    args=(server,)
                    )
    thread.start()
    # 把线程对象都存储到 threadlist中
    theadlist.append(thread)
for thread in theadlist:
    thread.join()
# print("\r" + 'EU-PVP-Official-TheIsland170 ', ARK[170][1],
#       '\nNA-PVP-Official-Aberration312', ARK[312][1],
#       '\nNA-PVP-Official-Valguero576  ', ARK[576][1],
#       '\nNA-PVP-Official-GenOne618    ', ARK[618][1], end="", flush=True)


# for ark in ARK:
#     print(ARK[ark][1])

#  EU-PVP-Official-TheIsland170                73043
#  NA-PVP-Official-Aberration312 - (v313.33    1756005
#  NA-PVP-Official-Valguero576 - (v313.33)     3753870
#  NA-PVP-Official-GenOne618 - (v313.33)   5753765


